package com.fog.student.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.fog.student.common.UserContext;
import com.fog.student.entity.Admin;
import com.fog.student.entity.SelectedCourse;
import com.fog.student.entity.Student;
import com.fog.student.mapper.AdminMapper;
import com.fog.student.mapper.StudentMapper;
import com.fog.student.service.AdminService;
import com.fog.student.service.SelectedCourseService;
import com.fog.student.service.StudentService;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.time.LocalTime;
import java.util.List;

@Service
public class StudentServiceImpl extends ServiceImpl<StudentMapper, Student> implements StudentService {

    @Resource
    private SelectedCourseService selectedCourseService;

    @Override
    @Transactional
    public void selectCourse(String ids) {
        SelectedCourse selectedCourse = new SelectedCourse();
        String[] idList = ids.split(",");
        for (String id : idList) {
            selectedCourse.setCourseId(Integer.parseInt(id));
            int idx = UserContext.getUserId().intValue();
            selectedCourse.setStudentId(idx);
            selectedCourseService.save(selectedCourse);
        }
    }

    @Override
    @Transactional
    public void cancelCourse(String ids) {
        String[] idList = ids.split(",");
        for (String id : idList) {
            QueryWrapper<SelectedCourse> selectedCourseQueryWrapper = new QueryWrapper<>();
            selectedCourseQueryWrapper.eq("course_id",id);
            int idx = UserContext.getUserId().intValue();
            selectedCourseQueryWrapper.eq("student_id",idx);
            selectedCourseService.remove(selectedCourseQueryWrapper);
        }
    }
}
